package com.DHH.Controller.map;

import com.DHH.Dto.Message;
import com.DHH.Util.FileUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.File;

/**
 * 注册协议控制类
 */
@Controller
@RequestMapping("/Admin/Protocol")
public class ProtocolController {
    @RequestMapping("/protocolPage")
    public String protocolPage(HttpServletRequest request, ModelMap modelMap)
    {     String strDirPath = request.getSession().getServletContext().getRealPath("/");
            strDirPath+= File.separatorChar+"public"+File.separatorChar+"protocol.txt";
           String str=FileUtil.readFile(strDirPath);
            modelMap.put("protocol",str);
            System.out.println(str);
            return "admin/system/protocol.jsp";
    }
    @RequestMapping("/saveProtocol")
    @ResponseBody
    public Message saveProtocol(@RequestParam String protocol, HttpServletRequest request)
    {
        Message message=new Message();
        String strDirPath = request.getSession().getServletContext().getRealPath("/");
        strDirPath+= File.separatorChar+"public"+File.separatorChar+"protocol.txt";
       if(FileUtil.writeFile(strDirPath,protocol))
        {
            message.setMsg("保存成功");
            message.setState(true);
            return message;
        }else{
           message.setMsg("保存失败");
           return message;
     }

    }
    @RequestMapping("/readProtocol")
    @ResponseBody
    public String readProtocol(HttpServletRequest request)
    {
        Message message=new Message();
        String strDirPath = request.getSession().getServletContext().getRealPath("/");
        strDirPath+= File.separatorChar+"public"+File.separatorChar+"protocol.txt";
       return  FileUtil.readFile(strDirPath);

    }
}
